package com.aier.admin.entity.mapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

import com.aier.admin.entity.SysBase;

public class SysBaseRowMapper implements RowMapper<SysBase> {

    @SuppressWarnings("unused")
    private int type;

    public SysBaseRowMapper() {
    }

    public SysBaseRowMapper(int type) {
        this.type = type;
    }

    @Override
    public SysBase mapRow(ResultSet rs, int rowNum) throws SQLException {
        return parseSysBase(new SysBase(), rs);
    }

    public static SysBase parseSysBase(SysBase sb, ResultSet rs) throws SQLException {
        return parseSysBase(sb, rs, null);
    }

    public static SysBase parseSysBase(SysBase sb, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }
        try {
            sb.setBid(rs.getInt(su+"bid"));
        } catch (SQLException e) {
            sb.setBid(null);
        }

        try {
            sb.setName(rs.getString(su+"name"));
        } catch (SQLException e) {
            sb.setName(null);
        }
        try {
            sb.setValue(rs.getString(su+"value"));
        } catch (SQLException e) {
            sb.setValue(null);
        }
        try {
            sb.setDes(rs.getString(su+"des"));
        } catch (SQLException e) {
            sb.setDes(null);
        }
    
        
        return sb;
    }

    public static Map<String, Object> getPar(Map<String, Object> par, SysBase sb) {
        par.put("bid", sb.getBid());
        par.put("name", sb.getName());
        par.put("value", sb.getValue());
        par.put("des", sb.getDes());

        return par;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args, SysBase sb) {
        args.put("bid",sb.getBid());
        return args;
    }

    /**
     * 
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final SysBase sb) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(sb),
                        new String[] { "bid" });
                parsePS(ps, sb);
                return ps;
            }

        };
    }

    private static String parseSQL(SysBase sb) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into sys_base(");
        int count = 0;
        if (null != sb.getName()) {
            sql.append("name,");
            count++;
        }

        if (null != sb.getValue()) {
            sql.append("value,");
            count++;
        }

        if (null != sb.getDes()) {
            sql.append("des,");
            count++;
        }
    
        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }

    private static void parsePS(PreparedStatement ps, SysBase sb)
        throws SQLException {
        int count = 1;
        if (null != sb.getName()) {
            ps.setString(count++,sb.getName());
        }
        
        if (null != sb.getValue()) {
            ps.setString(count++,sb.getValue());
        }


        if (null != sb.getDes()) {
            ps.setString(count++, sb.getDes());
        }
    
    }

}
